<?php
/**
 * 目录文件
 * @author Yangdong Zhang <zhangyangdong@gouuse.cn>
 * @time 2018/10/17 14:57
 */
namespace App\Model;

class NoteBook extends Model
{

    /**
     * 获取notebook列表
     * @return array
     * @throws \Exception
     */
    public function getNoteBookList()
    {
        $data = $this->where(['user_id' => $this->user_id])->select()->toArray();

        $tmp = [];
        $parent = [];
        foreach ($data as $k => $v) {
            if ($v['parent_id']) {
                $tmp[$v['parent_id']][] = $v;
            } else {
                $parent[] = $v;
            }
        }

        $dataList = [];
        foreach ($parent as $k => $v) {
            $v['child'] = $tmp[$v['id']] ?? [];
            $dataList[] = $v;
        }
        return $dataList;
    }

    /**
     * 获取最小的一条文本记录
     * @return int
     */
    public function getMinId()
    {
        $id = $this->where(['user_id' => $this->user_id])->where('parent_id>0 or (parent_id=0 and type=0)')->min('id');
        return (int)$id;
    }
}